﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;

namespace Xinnos.Lca.Yacht.Analyzer.View
{
    internal class MasterViewModel:INotifyPropertyChanged
    {
        private static MasterViewModel mInstance = null;
        public event PropertyChangedEventHandler PropertyChanged;
        private WorkBreakdownStructureTabPanel mStructure = null;
        private EnvironmentalProfilesPanel mProfile = null;
        private EnvironmentalProfilesGraphPanel mPie = null;
        private ComponentsPanel mComponent = null;
        private ObjectRatePanel mAssessment = null;
        private AssessmentGraphPanel mGraph = null;
        private Dictionary<Type, CommonDialog> mDialogs = null;
        private string mTitle = null;

        public bool Structure
        {
            get { return null == mStructure ? false : true; }
            set
            {
                if (null == mStructure)
                {
                    mStructure = new WorkBreakdownStructureTabPanel();
                    CommonDialog _dialog = new CommonDialog(mStructure);
                    _dialog.Title = "Work Breakdown Structure";
                    _dialog.Width = 300;
                    _dialog.Height = 600;
                    _dialog.Show();
                    mDialogs.Add(mStructure.GetType(), _dialog);
                }
                else
                {
                    RemoveDialog(mStructure);
                    mStructure = null;
                }
                RaisePropertyChanged("Structure");
            }
        }
        public bool Profile
        {
            get { return null == mProfile ? false : true; }
            set
            {
                if (null == mProfile)
                {
                    mProfile = new EnvironmentalProfilesPanel();
                    CommonDialog _dialog = new CommonDialog(mProfile);
                    _dialog.Title = "Environmental Profile";
                    _dialog.Width = 500;
                    _dialog.Height = 400;
                    _dialog.Show();
                    mDialogs.Add(mProfile.GetType(), _dialog);
                }
                else
                {
                    RemoveDialog(mProfile);
                    mProfile = null;
                }
                RaisePropertyChanged("Profile");
            }
        }
        public bool Pie
        {
            get { return null == mPie ? false : true; }
            set
            {
                if (null == mPie)
                {
                    mPie = new EnvironmentalProfilesGraphPanel();
                    CommonDialog _dialog = new CommonDialog(mPie);
                    _dialog.Title = "Environmental Profile";
                    _dialog.Width = 600;
                    _dialog.Height = 600;
                    _dialog.Show();
                    mDialogs.Add(mPie.GetType(), _dialog);
                }
                else
                {
                    RemoveDialog(mPie);
                    mPie = null;
                }
                RaisePropertyChanged("Pie");
            }
        }
        public bool Component
        {
            get { return null == mComponent ? false : true; }
            set
            {
                if (null == mComponent)
                {
                    mComponent = new ComponentsPanel();
                    CommonDialog _dialog = new CommonDialog(mComponent);
                    _dialog.Title = "Components";
                    _dialog.Width = 600;
                    _dialog.Height = 600;
                    _dialog.Show();
                    mDialogs.Add(mComponent.GetType(), _dialog);
                }
                else
                {
                    RemoveDialog(mComponent);
                    mComponent = null;
                }
                RaisePropertyChanged("Component");
            }
        }
        public bool Assessment
        {
            get { return null == mAssessment ? false : true; }
            set
            {
                if (null == mAssessment)
                {
                    mAssessment = new ObjectRatePanel();
                    CommonDialog _dialog = new CommonDialog(mAssessment);
                    _dialog.Title = "Assessment";
                    _dialog.Width = 800;
                    _dialog.Height = 600;
                    _dialog.Show();
                    mDialogs.Add(mAssessment.GetType(), _dialog);
                }
                else
                {
                    RemoveDialog(mAssessment);
                    mAssessment = null;
                }
                RaisePropertyChanged("Assessment");
            }
        }

        public bool Graph
        {
            get { return null == mGraph ? false : true; }
            set
            {
                if (null == mGraph)
                {
                    mGraph = new AssessmentGraphPanel();
                    CommonDialog _dialog = new CommonDialog(mGraph);
                    _dialog.Title = "Assessment";
                    _dialog.Width = 1000;
                    _dialog.Height = 500;
                    _dialog.Show();
                    mDialogs.Add(mGraph.GetType(), _dialog);
                }
                else
                {
                    RemoveDialog(mGraph);
                    mGraph = null;
                }
                RaisePropertyChanged("Graph");
            }
        }

        public ObjectRatePanel Rates
        {
            get { return mAssessment; }
        }

        public string Title
        {
            get { return mTitle; }
        }

        private MasterViewModel()
        {
            if (null == mDialogs) mDialogs = new Dictionary<Type, CommonDialog>();
            mTitle = ProgramProfileViewModel.GetInstance().DetailName;
            RaisePropertyChanged("Title");
        }
        public static MasterViewModel GetInstance()
        {
            if (null == mInstance) mInstance = new MasterViewModel();
            return mInstance;
        }

        private void RemoveDialog(object content)
        {
            Type _type = content.GetType();
            CommonDialog _dialog = mDialogs[_type];
            _dialog.Close();
            mDialogs.Remove(_type);
        }

        private void RaisePropertyChanged(string strPropertyName)
        {
            PropertyChangedEventHandler _handler = this.PropertyChanged;
            if (null != _handler) _handler(this, new PropertyChangedEventArgs(strPropertyName));
        }
    }
}